#include "project.h"

void mcp4911Write(uint8_t portno, uint16_t dacValue) {

	uint8_t writeEnable = FALSE,    //
			buffered = FALSE,    //
			gain = TRUE,    //
			shutdown = TRUE;

	uint8_t dacMsb, dacLsb;
//	dacValue=dacValue%1024;
	dacMsb = (writeEnable << 7) |    //shift the control and data appropriately.
			(buffered << 6) |    //The data is then logical OR'd together.
			(gain << 5) |    //j
			(shutdown << 4) |    //j
			((dacValue & 0x03c0) >> 6);    //adjusted for the mcp4911

	dacLsb = ((dacValue & 0x003f) << 2);
	cbi(PORTC,portno);
	_delay_us(10);
	spiWriteByte(dacMsb);
	spiWriteByte(dacLsb);
	sbi(PORTC,portno);
}

